package xmlPackage;
import java.util.HashMap;

import javax.xml.stream.XMLStreamReader;

import mapPackage.IGraphElement;

import org.w3c.dom.Node;


public class SFactoryGraph 
{
	private static SFactoryGraph instance =null;
	
	private HashMap<String, IGraphElement> mapFactory = new HashMap<String, IGraphElement>();
	
	private SFactoryGraph(  )
	{
	}
	
	public static SFactoryGraph GetInstance()
	{
		if( instance == null ){ instance = new SFactoryGraph(); }
		return instance;
	}

	
	public boolean registerGraphElement( String st , IGraphElement el )
	{
		this.mapFactory.put(st, el );return true;
	}
	
	public IGraphElement getGraphElement( XMLStreamReader reader )
	{
		String nomElement = reader.getLocalName();
		
		//System.out.println("demande de creation de ->"+nomElement);
		
		IGraphElement g = this.mapFactory.get(nomElement);
		if ( null == g ) 
		{
			try 
			{
				String classe = nomElement.substring(0,1).toUpperCase()+nomElement.substring(1).toLowerCase();
				classe = "mapPackage."+classe;
				
				ClassLoader.getSystemClassLoader().loadClass(classe).getField("registered").get(null);
				g = this.mapFactory.get(nomElement);
			} 
			catch (Exception e) 
			{
				e.printStackTrace();
			} 
		}
		
		
		if ( null != g )
		{ 
			g = g.create(reader);
			if(g==null){System.err.println("null ici");}
		}
		
		return g;
		
	}
	
}
